import { IRawMsg } from "@/chatlog_types/IRawMsg";
import { friendly_datetime_2 } from "@/utils/friendly_time_text";
import { Chat } from "@/views/Chat";
import dayjs from "dayjs";
import { throttle } from "lodash";
import { RawMsgType } from "../../views/ChatView/RawMsgType";
import { ISessionItem } from "../session";
import { BaseChatMgr, ISession } from "./BaseChatMgr";
import { MessageLoader } from "./MessageLoader";
import { RemoteMessageLoader } from "./RemoteMessageLoader";

export class RemoteChatMgr extends BaseChatMgr {
  static readonly TAG: string = 'RemoteChatMgr';
  readonly TAG: string = RemoteChatMgr.TAG
  readonly listview: Chat.MessageListView<IRawMsg>
  readonly loader: MessageLoader = new RemoteMessageLoader(this);
  constructor(session: ISession) {
    super(session);
    this.listview = new Chat.MessageListView(void 0);
    this.listview.renderAfterSystemMsg = (msg: Chat.Message<IRawMsg>) => {
      return RawMsgType.renderAfterBubble(msg.raw)
    }
    this.listview.renderAfterBubble = (msg: Chat.Message<IRawMsg>) => {
      return RawMsgType.renderAfterBubble(msg.raw)
    }
    this.listview.formatDate = (n) => friendly_datetime_2(dayjs(), dayjs(n))
  }
  override get latest(): Chat.Message<IRawMsg> | undefined {
    return this.listview.messages[this.listview.messages.length - 1];
  }
  override get earliest(): Chat.Message<IRawMsg> | undefined {
    return this.listview.messages[0];
  }
  override get messages_length(): number {
    return this.listview.messages.length;
  }
  override get messages(): Chat.Message<IRawMsg>[] {
    return this.listview.messages
  }
  override clear_msgs() {
    this.listview.setMessages([]);
    this.listview.parent?.scrollTo({ top: this.listview.parent.scrollHeight });
  }
  override append_msgs(msgs: Chat.Message<IRawMsg>[]) {
    this.listview.appendMessages(msgs);
  }
  set_parent(ele: HTMLDivElement | undefined) {
    if (ele == this.listview.parent) return;
    this.listview.set_parent(ele)
  }
  override insert_msgs(index: number, msgs: Chat.Message<IRawMsg>[]) {
    const { parent } = this.listview;
    const old_scroll_height = parent?.scrollHeight || 0;
    this.listview.insertMessages(index, msgs);
    if (parent) parent.scrollTo({ top: parent.scrollTop + parent.scrollHeight - old_scroll_height })
  }
  override set_msgs(msgs: Chat.Message<IRawMsg>[]) {
    this.listview.setMessages(msgs);
  }
  override scroll_to_bottom(behavior: "auto" | "instant" | "smooth" = 'smooth') {
    const sv = this.listview.parent;
    if (!sv) return;
    sv.scrollTo({ top: sv.scrollHeight - sv.clientHeight, behavior: behavior });
  }
  override is_reach_bottom() {
    const sv = this.listview.parent;
    return !!sv && sv.scrollHeight - sv.scrollTop === sv.clientHeight;
  }
  override before_history_load() {
    this.listview.parent?.removeEventListener('scroll', this.on_scroll);
  }
  override after_history_load() {
    this.listview.parent?.addEventListener('scroll', this.on_scroll);
  }
  on_scroll = throttle(() => {
    const { parent } = this.listview;
    if (!parent || parent.scrollTop > 2000 || this.history_loading) return;
    this.load_history();
  }, 1000)

}